<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<link th:href="@{/css/style.css}" rel="stylesheet" type="text/css" />
<script type="text/javascript" th:src="@{/js/jquery.js}"></script>
</head>

<body>
<div class="place"> <span>位置：</span>
  <ul class="placeul">
    <li>营销管理</li>
    <li>订购单管理</li>
    <li>添加</li>
  </ul>
</div>
<div class="formbody">
  <form th:action="@{/order/addOrder}" method="post" id="orderFrom">
      <div class="formtitle"><span>订购单信息</span></div>
      <ul class="forminfo">
          <li>
              <label>订单编号</label>
              <input name="orderid" id="orderid" type="text" value="DJ201711180001" readonly="readonly" class="roinput" />
              <i>自动生成不能编辑</i>
          </li>
          <li>
              <label>客户姓名</label>
              <input type="hidden" name="customid" id="customid" >
              <input name="customname" type="text" id="customname" class="dfinput" />
              <i id="customNameHint">不能为空</i>
          </li>
          <li>
              <label>订购时间</label>
<!--              <input name="ordertime" id="ordertime" type="text" value="2017-11-18 15:36:10" readonly="readonly" class="roinput" />-->
              <input name="ordertimeStr" id="ordertimeStr" type="text" value="2017-11-18 15:36:10" readonly="readonly" class="roinput" />
              <i>不能编辑</i>
          </li>
          <li>
              <label>创建人</label>
              <input type="hidden" name="operatorid" th:value="${session.user.uid}"/>
              <input  type="text" th:value="${session.user.uname}" readonly="readonly" class="roinput" />
              <i>不能编辑</i>
          </li>
          <li>
              <label>审核状态</label>
              <input type="text" name="status" value="未审核" readonly="readonly" class="roinput" />
              <i>初始状态，不能编辑</i>
          </li>
          <li>
              <label>总金额</label>
              <input name="ordermoney" id="ordermoney" type="text" readonly="readonly" class="roinput" />
              <i>自动运算，不能编辑</i>
          </li>
          <li>
              <input type="button" value="新增" class="smallbtn" onclick="addOrderdetails()" />
              &nbsp;&nbsp;&nbsp;&nbsp;
              <input type="button" value="删除" class="smallbtn"  onclick="deleteOrderdetails()" />
              &nbsp;&nbsp;&nbsp;&nbsp;
              <input type="button" value="保存" class="smallbtn" onclick="submitAndVerify()" />
              &nbsp;&nbsp;&nbsp;&nbsp;
              <input type="button" value="返回" class="smallbtn" onclick="toBack()"/>
          </li>
      </ul>
      <table class="tablelist">
          <thead>
              <tr>
                  <th></th>
                  <th>序号</th>
                  <th>品牌</th>
                  <th>类型</th>
                  <th>型号</th>
                  <th>数量</th>
                  <th>单位</th>
                  <th>单价</th>
                  <th>金额</th>
              </tr>
          </thead>
          <tbody id="OrderdetailsList">
              <tr>
                  <td><input type="checkbox" name="OrderdetailsCheck" style="width: 20px"/></td>
                  <td class="bianhao">1</td>
                  <td>
                      <select name="brandId" class="brandId" >
                          <option value="">请选择</option>
                          <option th:each="brand:${brandList}" th:text="${brand.brandName}" th:value="${brand.brandId}"></option>
                      </select>
                  </td>
                  <td>
                      <select name="typeId" class="typeId" >
                          <option value="">请选择</option>
                      </select>
                  </td>
                  <td>
                      <select name="productId" class="productId" >
                          <option value="" >请选择</option>
                      </select>
                  </td>
                  <td><input name="purchaseNum" class="purchaseNum"  type="text" /></td>
                  <td>台</td>
                  <td><input name="proprice" class="proprice" type="text" /></td>
                  <td><input name="prototal" class="prototal" type="text" /></td>
              </tr>
          </tbody>
      </table>
  </form>
</div>
<script type="text/javascript">
	$('.tablelist tbody tr:odd').addClass('odd');
    function toBack(){
        window.location.href="/market/orderList";
    }

    function addOrderdetails (){
        $('#OrderdetailsList tr').last().clone(true).appendTo("tbody");
        $('#OrderdetailsList tr').last().find(".bianhao").html($('#OrderdetailsList tr').length)
        var options = " <option value=''>请选择</option>"

        $('#OrderdetailsList tr').last().children("td:eq(2)").find('.brandId').children("option:eq(0)").attr("selected",true);

        $('#OrderdetailsList tr').last().children("td:eq(3)").find('.typeId').html("");
        $('#OrderdetailsList tr').last().children("td:eq(3)").find('.typeId').html(options);

        $('#OrderdetailsList tr').last().children("td:eq(4)").find('.productId').html("");
        $('#OrderdetailsList tr').last().children("td:eq(4)").find('.productId').html(options);

        $('#OrderdetailsList tr').last().children("td:eq(5)").find('.purchaseNum').val("");
        $('#OrderdetailsList tr').last().children("td:eq(7)").find('.proprice').val("");
        $('#OrderdetailsList tr').last().children("td:eq(8)").find('.prototal').val("");
    }
    function deleteOrderdetails() {
        $("[name='OrderdetailsCheck']:checked").each(function (){
            $(this).parent().parent().remove()
        })
    }
    function submitAndVerify(){
        var customid = $("#customid").val();
        if(customid != null && customid != ""){
            $("#orderFrom").submit();
        }else {
            $("#customNameHint").text("用户名不存在或为空，请输入正确的客户名！")
        }
    }
    $("#customname").blur(function(){
        var customname = $("#customname").val();
        if(customname != null && customname != ""){
            $.ajax({
                url: "/order/queryCustomIsExist",
                data: {"customname": customname},
                dataType: "text",
                success:function (data){
                    if(data > 0 ){
                        $("#customid").val(data);
                        $("#customNameHint").text("用户名存在!!");
                    }else {
                        $("#customNameHint").text("用户名不存在！请重新输入");
                    }
                },
                error:function (){
                    alert(222);
                }
            });
        }else {
            $("#customNameHint").text("用户名不能为空")
        }
    })

    $("#OrderdetailsList").children("tr").each(function (){
        /*为了显示商品的类型*/
        $(this).children("td:eq(2)").change(function (){
            var brandId = $(this).find(".brandId").val();
            var tempThis = $(this);
            if(brandId != null && brandId != ""){
                $.ajax({
                    url: "/order/queryCTypeListByAjax",
                    data: {"brandId": brandId},
                    dataType: "JSON",
                    success:function (data){
                        /*清空类型数据*/
                        tempThis.next().find(".typeId").html("");
                        var options = "<option value=''>请选择</option>";
                        var arr=null;
                        if(typeof data=='string')
                            arr=JSON.parse(data);
                        else
                            arr=data;
                        for(let i in arr){
                            options += "<option value='"
                                + arr[i].typeId
                                + "'>"
                                + arr[i].typeName
                                + "</option>";
                        }
                        console.log(options);
                        tempThis.next().find(".typeId").html(options);
                    },
                    error:function (){
                        alert(222);
                    }
                });
            }else {
                /*清空类型数据*/
                tempThis.next().find(".typeId").html("");
                var options = "<option value=''>请选择</option>";
                tempThis.next().find(".typeId").html(options);
            }
        });
        /*为了显示对应的商品型号*/
        $(this).children("td:eq(3)").change(function (){
            var typeId = $(this).find(".typeId").val();
            var tempThis = $(this);
            if(typeId != null && typeId != ""){
                $.ajax({
                    url: "/order/queryProductListByAjax",
                    data: {"typeId": typeId},
                    dataType: "JSON",
                    success:function (data){
                        /*清空商品型号数据*/
                        tempThis.next().find(".productId").html("");
                        var options = "<option value=''>请选择</option>";
                        var arr=null;
                        if(typeof data=='string')
                            arr=JSON.parse(data);
                        else
                            arr=data;

                        for(let i in arr){
                            options += "<option value='"
                                + arr[i].productId
                                + "'>"
                                + arr[i].productModel
                                + "</option>";
                        }
                        console.log(options);
                        tempThis.next().find(".productId").html(options);
                    },
                    error:function (){
                        alert(222);
                    }
                });
            }else {
                /*清空商品型号数据*/
                tempThis.next().find(".productId").html("");
                var options = "<option value=''>请选择</option>";
                tempThis.next().find(".productId").html(options);
            }
        });
        /*为了显示商品对应的单价*/
        $(this).children("td:eq(4)").change(function (){
            var productId = $(this).find(".productId").val();
            var tempThis = $(this);
            if(productId != null && productId != ""){
                $.ajax({
                    url: "/order/queryProductPriceByAjax",
                    data: {"productId": productId},
                    dataType: "text",
                    success:function (data){
                        /*清空商品单价数据*/
                        tempThis.next().next().next().find(".proprice").val(data);
                    },
                    error:function (){
                        alert(222);
                    }
                });
            }else {
                /*清空商品单价数据*/
                tempThis.next().next().next().find(".proprice").val("");
            }
        });
        /*输入数量，对金额进行赋值*/
        $(this).children("td:eq(5)").find(".purchaseNum").keyup(function (){
            var purchaseNum = $(this).val();
            var price = $(this).parent().next().next().find(".proprice").val();
            var prototal = 0;
            if(purchaseNum!=null && purchaseNum != ""){
                if(price!=null && price != ""){
                    prototal = price * purchaseNum;
                }
            }
            $(this).parent().next().next().next().find(".prototal").val(prototal);

            var ordermoney = 0 ;
            $(".prototal").each(function(){
                var money = $(this).val();
                if(money!=null && money != "" && money!=0){
                    ordermoney = Number(ordermoney) + Number(money);
                }
            })

            $("#ordermoney").val(ordermoney);

        });

    })
    var nowTime = new Date();
    var year = nowTime.getFullYear();
    var month = nowTime.getMonth()+1;
    var day = nowTime.getDate();
    var hour = nowTime.getHours();
    var minutes = nowTime.getMinutes();
    var second = nowTime.getSeconds();
    if(month>0 && month<10){
        month = "0"+month;
    }
    if(day>0 && day<10){
        day = "0"+day;
    }
    if(hour>0 && hour<10){
        hour = (hour-8+24)%24
        hour = "0"+hour;
    }
    if(minutes>0 && minutes<10){
        minutes = "0"+minutes;
    }
    if(second>0 && second<10){
        second = "0"+second;
    }
    /* 订购时间 */
    var nowDate = year+"-"+month+"-"+day+" "+hour+":"+minutes+":"+second;
    $("#ordertimeStr").val(nowDate);
    /* 订单编号 */
    var orderid = "DJ"+year+month+day+hour+minutes+second;
    $("#orderid").val(orderid);




</script>
</body>
</html>
